package com.sdu.shanyimisapi.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.hutool.core.bean.BeanUtil;
import com.sdu.shanyimisapi.common.PageUtils;
import com.sdu.shanyimisapi.common.R;
import com.sdu.shanyimisapi.controller.form.*;
import com.sdu.shanyimisapi.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lihaopeng
 * @version 1.0
 * @project shanyi-mis-api
 * @description 用户
 * @since 2024/4/13 22:16
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 分页查询用户
     *
     * @param form
     * @return
     */
    @PostMapping("/searchByPage")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "USER:SELECT"}, mode = SaMode.OR)
    public R searchByPage(@RequestBody @Valid SearchUserByPageForm form) {
        Map param = BeanUtil.beanToMap(form);
        int page = form.getPage();
        int length = form.getLength();
        int start = (page - 1) * length;
        param.put("start", start);
        PageUtils pageUtils = userService.searchByPage(param);
        return R.ok().put("result", pageUtils);
    }

    /**
     * 根据id查询用户
     *
     * @param form
     * @return
     */
    @PostMapping("/searchById")
    @SaCheckLogin
    @SaCheckPermission(value = {"ROOT", "USER:SELECT"}, mode = SaMode.OR)
    public R searchById(@RequestBody @Valid SearchUserByIdForm form) {
        HashMap map = userService.searchById(form.getId());
        return R.ok(map);
    }

}
